<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

class BlogAction extends Action{
	//博客首页
    public function index(){
        $this->display();
    }

	//添加博客页面
	//为编辑提供信息
    public function addBlog(){
		$id=$this->_get('id');	//blogID
		if(!$this->_post('Submit')){
			$m = M('bloglog')->where("id={$id}")->find();
			$m['tag']=1;
			$this->assign('info',$m);
		}
		$uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$team = M()->query("SELECT a.TeamId,a.InstructorId,b.Id,b.Name FROM teaminstructors a,team b WHERE b.Id=a.TeamId AND a.InstructorId={$uid}");
		$this->assign('team',$team);
		$this->display();
	}
	
	//添加,编辑博客方法
	//根据表单隐藏blogid选项判断是否编辑
	public function addBlogCom(){
		$id=$this->_post('blogid');
		if(!$id){
			$arr['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$arr['TrueName'] = $_SESSION[C('USER_AUTH_KEY')]['TrueName'];
			$arr['Title'] = $this->_post('Title');
			$arr['Tags'] = $this->_post('Tags');
			$arr['Content'] = $this->_post('Content',false);
			$arr['CreateDate'] = date("Y-m-d H:i:s");
			$arr['IsDeleted'] = 0;
			$blogid = add_blog($arr);
			//如果有团队也要写入teamblogs中
			if(!!$team = $this->_post('Team')){
				$data['BlogId'] = $blogid;
				$data['TeamId'] = $team;
				$data['IsApproved'] = 0;
				M('Teamblogs')->add($data);
			}
			$this->success('添加成功', 'index');
		}else{
			$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$m = M('bloglog');
			$data['Title']=$this->_post('Title');
			$data['Content']=$this->_post('Content');
			$data['Tags']=$this->_post('Tags');
			$m->where("Id={$id}")->save($data);
			$this->redirect("/Student/Home/index/id/{$uid}");
		}
	}	
	//删除博文
	public function delBlog(){
		echo $id=$this->_get('id');	//blogID
		$uid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$m = M('bloglog');
		$data['IsDeleted']='1';	//0显示 1删除
		$res=$m->where("Id={$id}")->save($data);
		$this->redirect("/Student/Home/index/id/{$uid}");
	}
	//文章详情页面
	public function article(){
		$id = $this->_get('id');
		$data = new BlogModel();
		$article = $data->getArticle($id);
		$data->getViewCount($id);
		$this->assign('article',$article);
		//相关博文
		$tags = $article[0]['Tags'];
		$aboutBlog = $data->getAboutBlog($tags,$id);
		Load('extend');//加载截取函数
		$this->assign('aboutBlog',$aboutBlog);
		$this->display();
	}
	
    public function getSchoolList(){
		$this->display();
    }
    public function nameValid(){
      //  $Model  =   D('Form');
      //  $this->assign('vo',$Model->getDetail($id));
     
    }    
    public function registerCom(){
      //  $Model  =   D('Form');
      //  $this->assign('vo',$Model->getDetail($id));
        $this->display();
    }
    public function detailForm()
    {
       $this->display();
    }
    public function getSpecialFieldList()
    {
    	$this->display();
    }
	
	//博文列表
	public function getBlogList(){
		Load('extend');//加载截取函数
		@$page = $this->_get('page');
		if(!intval($page))
			$page=1;
		$trade = new BlogModel($page);
		if($trade->page-1>0){
			$this->assign('qian',$trade->page-1);			//前一页
		}
		if($trade->page+1<$trade->pagenum){
			$this->assign('hou',$trade->page+1);			//后一页
		}
		$this->assign('pagefor',$trade->pagefor);		//页号的显示
		$this->assign('pagenum',$trade->pagenum);		//总页数
		$this->assign('page',$trade->page);				//当前页
		$this->assign('bplist',$trade->bplist);
		$this->display();
	}
	
	//添加博文评论
    public function addBlogComment(){
		$comment = M('Blogcomment');
		$arr['Content'] = $this->_post('comment');
		$arr['LogId'] = $this->_post('articleid');
		$arr['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$arr['CreateDate'] = date('Y-m-d h:m:s');
		$arr['OrgName'] = $_SESSION[C('USER_AUTH_KEY')]['OrgName'];
		$arr['UserName'] = $_SESSION[C('USER_AUTH_KEY')]['UserName'];
		echo $comment->add($arr);
		//添加评论数
		M()->query("UPDATE bloglog SET CommentCount = CommentCount+1 WHERE Id={$arr['LogId']}");
	}
	
	//显示博文评论
	public function BlogComment(){
		$comment = M();
		$logId = $this->_get('id');
		@$page = $this->_get('page');
		if(!intval($page))
			$page=1;
		$trade = new CommentModel($page,$logId);
		if($trade->page-1>0){
			$this->assign('qian',$trade->page-1);			//前一页
		}
		if($trade->page+1<$trade->pagenum){
			$this->assign('hou',$trade->page+1);			//后一页
		}
		$this->assign('pagefor',$trade->pagefor);		//页号的显示
		$this->assign('pagenum',$trade->pagenum);		//总页数
		$this->assign('page',$trade->page);
		$this->assign('logId',$logId);
		$this->assign('commentlist',$trade->bplist);
		$this->display();
	}
}
